#include "pagedoorparameters.h"
#include "ui_pagedoorparameters.h"

PageDoorParameters::PageDoorParameters(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageDoorParameters)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_pNumKeyBoard = new NumberKeyBoardDot(this);
    m_pNumKeyBoard->move(880,200);
    m_pNumKeyBoard->setFocusPolicy(Qt::NoFocus);
    connect(m_pNumKeyBoard,SIGNAL(setNumber(int)),this,SLOT(input_slot(int)));
    connect(m_pNumKeyBoard,SIGNAL(backspace()),this,SLOT(backspace_slot()));
    connect(m_pNumKeyBoard,SIGNAL(cancel()),this,SLOT(cancel_slot()));
    connect(m_pNumKeyBoard,SIGNAL(dot()),this,SLOT(dot_slot()));
    m_pNumKeyBoard->hide();

    inputlist.append(ui->lineEdit_OpenDoorDelayTime);
    inputlist.append(ui->lineEdit_CloseDoorDelayTime);
    inputlist.append(ui->lineEdit_OpenDoorTime);
    inputlist.append(ui->lineEdit_CloseDoorTime);
    inputlist.append(ui->lineEdit_ObstacleDetectFrequency);
    inputlist.append(ui->lineEdit_DetectObstacleOpenDoorDistance);
    inputlist.append(ui->lineEdit_DetectObstacleAutoCloseDoorDelayTime);
    inputlist.append(ui->lineEdit_DetectObstacleFinallyOpenDoorDistance);
    inputlist.append(ui->lineEdit_DetectObstacleKeepCloseDoorTime);
    inputlist.append(ui->lineEdit_CloseForce_1);
    inputlist.append(ui->lineEdit_CloseForce_2);
    inputlist.append(ui->lineEdit_CloseForce_3);
    inputlist.append(ui->lineEdit_CloseForce_4);
    inputlist.append(ui->lineEdit_CloseForce_5);

    for(int i=0; i<inputlist.size(); ++i)
        inputlist.at(i)->installEventFilter(this);

    ui->Btn_Set->setEnabled(false);

    m_TightCloseList.append(ui->label_CloseForce_1);
    m_TightCloseList.append(ui->label_CloseForce_2);
    m_TightCloseList.append(ui->label_CloseForce_3);
    m_TightCloseList.append(ui->label_CloseForce_4);
    m_TightCloseList.append(ui->label_CloseForce_5);

    setRegex();
}

bool PageDoorParameters::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<inputlist.size(); ++i)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            if(obj == inputlist.at(i))
            {
                inputlist.at(i)->setStyleSheet("background-color: rgb(100,255,255); border: 1px solid white;font:20px 'Arial';");
                m_pNumKeyBoard->show();
                m_pFocusLinedit = inputlist.at(i);
            }
            else
                inputlist.at(i)->setStyleSheet("background-color: white; border: 1px solid white;font:20px 'Arial';");
        }
    }
    return QWidget::eventFilter(obj, event);
}

PageDoorParameters::~PageDoorParameters()
{
    delete ui;
}

void PageDoorParameters::setRegex()
{
    ui->lineEdit_OpenDoorDelayTime->setValidator(new QRegExpValidator(QRegExp("^[0-2][.][1-9]|3$"),this));
    ui->lineEdit_CloseDoorDelayTime->setValidator(new QRegExpValidator(QRegExp("^[0-2][.][1-9]|3$"),this));
    ui->lineEdit_OpenDoorTime->setValidator(new QRegExpValidator(QRegExp("^2[.][5-9]|[3-4][.][0-9]|5$"),this));
    ui->lineEdit_CloseDoorTime->setValidator(new QRegExpValidator(QRegExp("^2[.][5-9]|[3-4][.][0-9]|5$"),this));
    ui->lineEdit_ObstacleDetectFrequency->setValidator(new QRegExpValidator(QRegExp("^[1-5]$"),this));
    ui->lineEdit_DetectObstacleOpenDoorDistance->setValidator(new QRegExpValidator(QRegExp("^[0-9]|[1-9][0]|[1-9][0-9][0]|1[0-2][0-9][0]|1300$"),this));
    ui->lineEdit_DetectObstacleAutoCloseDoorDelayTime->setValidator(new QRegExpValidator(QRegExp("^[0-8]$"),this));
    ui->lineEdit_DetectObstacleFinallyOpenDoorDistance->setValidator(new QRegExpValidator(QRegExp("^[0-9]|[1-9][0]|[1-9][0-9][0]|1[0-2][0-9][0]|1300$"),this));
    ui->lineEdit_DetectObstacleKeepCloseDoorTime->setValidator(new QRegExpValidator(QRegExp("^0[.][5-9]|[1-2][.][0-9]|3$"),this));
    ui->lineEdit_CloseForce_1->setValidator(new QRegExpValidator(QRegExp("^1[5-9][0]|2[0-9][0]|300$"),this));
    ui->lineEdit_CloseForce_2->setValidator(new QRegExpValidator(QRegExp("^1[5-9][0]|2[0-9][0]|300$"),this));
    ui->lineEdit_CloseForce_3->setValidator(new QRegExpValidator(QRegExp("^1[5-9][0]|2[0-9][0]|300$"),this));
    ui->lineEdit_CloseForce_4->setValidator(new QRegExpValidator(QRegExp("^1[5-9][0]|2[0-9][0]|300$"),this));
    ui->lineEdit_CloseForce_5->setValidator(new QRegExpValidator(QRegExp("^1[5-9][0]|2[0-9][0]|300$"),this));
}

void PageDoorParameters::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

void PageDoorParameters::input_slot(int number)
{
    QKeyEvent numKey(QEvent::KeyPress, (Qt::Key)(Qt::Key_0+number), Qt::NoModifier, QString::number(number));
    QApplication::sendEvent(m_pFocusLinedit, &numKey);
}

void PageDoorParameters::backspace_slot()
{
    QKeyEvent backspaceKey(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier);
    QApplication::sendEvent(m_pFocusLinedit,&backspaceKey);
}

void PageDoorParameters::cancel_slot()
{
    m_pNumKeyBoard->hide();
}

void PageDoorParameters::dot_slot()
{
    m_pFocusLinedit->insert(".");
}

void PageDoorParameters::setDelOpen(uint16_t delay_time)
{
    if(delay_time < 1 || delay_time > 30)
        return;
    ui->label_OpenDoorDelayTime->setText(QString("%1 s").arg(delay_time*0.1));
}

void PageDoorParameters::PageDoorParameters::setDelClose(uint16_t delay_time)
{
    if(delay_time < 1 || delay_time > 30)
        return;
    ui->label_CloseDoorDelayTime->setText(QString("%1 s").arg(delay_time*0.1));
}

void PageDoorParameters::setOpenTime(uint16_t time)
{
    if(time < 25 || time > 50)
        return;
    ui->label_OpenDoorTime->setText(QString("%1 s").arg(time*0.1));
}

void PageDoorParameters::setCloseTime(uint16_t time)
{
    if(time < 25 || time > 50)
        return;
    ui->label_CloseDoorTime->setText(QString("%1 s").arg(time*0.1));
}

void PageDoorParameters::setObstNumb(uint16_t count)
{
    if(count < 1 || count > 5)
        return;
    ui->label_ParObstNumb->setText(QString("%1 次").arg(count));
}

void PageDoorParameters::setOpenWidthObst(uint16_t width)
{
    if(width > 130)
        return;
    ui->label_ParOpenWidthObst->setText(QString("%1 mm").arg(width*10));
}

void PageDoorParameters::setSusTimObst(uint16_t time)
{
    if(time > 80)
        return;
    ui->label_ParSusTimObst->setText(QString("%1 s").arg(time*0.1));
}

void PageDoorParameters::setFinOpenWidthObst(uint16_t width)
{
    if(width > 130)
        return;
    ui->label_FinOpenWidthObst->setText(QString("%1 mm").arg(width*10));
}

void PageDoorParameters::setAppTimObst(uint16_t time)
{
    if(time < 1 || time > 30)
        return;
    ui->label_AppTimObst->setText(QString("%1 s").arg(time*0.1));
}

void PageDoorParameters::setTightClose(uint8_t index,uint16_t force)
{
    if(index >= 6 || force < 15 || force >30)
        return;
    m_TightCloseList[index]->setText(QString("%1 N").arg(force*10));
}

void PageDoorParameters::setPageActive(bool xActive)
{
    ui->Btn_Set->setEnabled(xActive);
}

void PageDoorParameters::on_Btn_Set_clicked()
{
    if(ui->lineEdit_OpenDoorDelayTime->text()=="")
    {
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入开门延时时间");
    }else if(ui->lineEdit_CloseDoorDelayTime->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入关门延时时间");
    }else if(ui->lineEdit_OpenDoorTime->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入开门时间");
    }else if(ui->lineEdit_CloseDoorTime->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入关门时间");
    }else if(ui->lineEdit_ObstacleDetectFrequency->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入障碍检测次数");
    }else if(ui->lineEdit_DetectObstacleOpenDoorDistance->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入检测到障碍后开门距离");
    }else if(ui->lineEdit_DetectObstacleAutoCloseDoorDelayTime->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入检测到障碍后自动关门延时时间");
    }else if(ui->lineEdit_DetectObstacleFinallyOpenDoorDistance->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入检测到障碍后最终打开门的距离");
    }else if(ui->lineEdit_DetectObstacleKeepCloseDoorTime->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入检测到障碍时保持关门力时间");
    }else if(ui->lineEdit_CloseForce_1->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入第1次关门力");
    }else if(ui->lineEdit_CloseForce_2->text()==""){
       ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入第2次关门力");
    }else if(ui->lineEdit_CloseForce_3->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入第3次关门力");
    }else if(ui->lineEdit_CloseForce_4->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入第4次关门力");
    }else if(ui->lineEdit_CloseForce_5->text()==""){
        ui->Btn_Set->setChecked(false);
        ui->errormsg->setText("请输入第5次关门力");
    }else{
        ui->errormsg->setText("");
        emit signal_DoorParam(0xAA);
        ui->Btn_Set->setEnabled(false);
        QTimer::singleShot(3000,this,[=](){
            emit signal_DoorParam(0x55);
            ui->Btn_Set->setEnabled(true);
        });
    }
}

uint16_t PageDoorParameters::getDelOpen() const
{
    return ui->lineEdit_OpenDoorDelayTime->text().toFloat()*10;
}
uint16_t PageDoorParameters::getDelClose() const
{
    return ui->lineEdit_CloseDoorDelayTime->text().toFloat()*10;
}
uint16_t PageDoorParameters::getOpenTime() const
{
    return ui->lineEdit_OpenDoorTime->text().toFloat()*10;
}
uint16_t PageDoorParameters::getCloseTime() const
{
    return ui->lineEdit_CloseDoorTime->text().toFloat()*10;
}
uint16_t PageDoorParameters::getObstNumb() const
{
    return ui->lineEdit_ObstacleDetectFrequency->text().toUInt();
}
uint16_t PageDoorParameters::getOpenWidthObst() const
{
    return ui->lineEdit_DetectObstacleOpenDoorDistance->text().toInt()/10;
}
uint16_t PageDoorParameters::getSusTimObst() const
{
    return ui->lineEdit_DetectObstacleAutoCloseDoorDelayTime->text().toFloat()*10;
}
uint16_t PageDoorParameters::getFinOpenWidthObst() const
{
    return ui->lineEdit_DetectObstacleFinallyOpenDoorDistance->text().toInt()/10;
}
uint16_t PageDoorParameters::getAppTimObst() const
{
    return ui->lineEdit_DetectObstacleKeepCloseDoorTime->text().toFloat()*10;
}
uint16_t PageDoorParameters::getTightClose(uint8_t index) const
{
    return inputlist[index+9]->text().toInt()/10;
}
